package com.jbp.controller;

import com.jbp.model.ArticleComment;
import com.jbp.model.ArticleCommentComment;
import com.jbp.service.ArticleCommentCommentService;
import com.jbp.service.KeywordService;
import com.jbp.utils.PageUtils;
import com.jbp.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Map;

/**
 * 评论的评论(ArticleCommentComment)表控制层
 * @author cc
 * @since 2020-08-25 14:37:34
 */
@Api(tags ="APP接口--动态-评论的评论" )
@RestController
@ApiIgnore
@RequestMapping("rest/articleCommentComment")
public class ArticleCommentCommentController {
    @Autowired
    private ArticleCommentCommentService articleCommentCommentService;

    @Autowired
    private KeywordService keywordService;
    /** 列表 */
    @ApiOperation(value="列表")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功",response = ArticleCommentComment.class),
            @ApiResponse(code = 201,message = "返回失败信息")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId",value = "评论id",paramType = "body"),
    })
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = articleCommentCommentService.queryPage(params);
        return R.ok().put("page", page);
    }

    /** 保存 */
    @ApiOperation(value="保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commentId",value = "评论id",paramType = "body"),
            @ApiImplicitParam(name = "replyUserId",value = "评论用户id",paramType = "body"),
            @ApiImplicitParam(name = "type",value = "类型 word 文本， image 图片， video 视频， voice 声音",paramType = "body"),
            @ApiImplicitParam(name = "content",value = "文本",paramType = "body"),
            @ApiImplicitParam(name = "files",value = "文件列表，多个文件用,分隔",paramType = "body"),
    })
    @PostMapping("/save")
    public R save(@RequestBody ArticleCommentComment articleCommentComment, @RequestAttribute("userId") Integer userId){
        keywordService.checkContent(articleCommentComment.getContent());
        articleCommentComment.setReaderId(userId);
        articleCommentCommentService.add(articleCommentComment);
        return R.ok(articleCommentComment);
    }


    /** 删除 */
    @ApiOperation(value="删除")
    @PostMapping("/delete")
    public R delete(@RequestBody Integer articleCommentCommentId, @RequestAttribute("userId") Integer userId){
        ArticleCommentComment articleCommentComment = articleCommentCommentService.getById(articleCommentCommentId);
        if (!userId.equals(articleCommentComment.getReaderId())) {
            return R.error("只有本人才能删除");
        }
        articleCommentCommentService.remove(articleCommentCommentId);
        return R.ok();
    }
}